package net.wanho.service.impl;

import cn.hutool.core.util.IdUtil;
import net.wanho.entity.User;
import net.wanho.mapper.UserMapper;
import net.wanho.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Author：汤小洋
 * Date：2023-08-24 9:48
 * Description：<描述>
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        // 1.生成随机盐
        user.setSalt(IdUtil.simpleUUID());
        // 2.对密码进行加密
        user.setPassword(new Md5Hash(user.getPassword(),user.getSalt(),1024).toHex());
        userMapper.insert(user);
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public User findRolesByUsername(String username) {
        return userMapper.selectRolesByUsername(username);
    }
}
